<?php

namespace app\admin\controller\erm;

use app\admin\{
    controller\Controllers,
    service\erm\BooksContents as BooksContentsService,
    model\erm\BooksContents as BooksContentsModel,
    service\erm\Books
};

class BooksContents extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(BooksContentsService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\erm\BooksContents.index');
        $list = $service->indexPaginate($get);
        $config = config('field.books_contents');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);
        return $this->fetch('erm/books_contents/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $config = config('field.books');
        $books = app('books_service')->alls();
        $this->assign([
            'status' => $config['status'],
            'books' => $books
        ]);
        return $this->fetch('erm/books_contents/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save( BooksContentsService $service )
    {
        $post = $this->request->only(['img_url','uk_audio_url','en_audio_url','name','profiles','bid','sort','status','phonogram'], 'post');
        $this->validate($post, 'app\admin\validate\erm\BooksContents.create');
        $bcId = $service->create($post);
        if ( false === $bcId ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(BooksContentsModel $booksContents, Books $bookService )
    {
        $config = config('field.books_contents');
        $books = $bookService->alls();
        $this->assign([
            'status' => $config['status'],
            'books' => $books,
            'data' => $booksContents
        ]);
        return $this->fetch('erm/books_contents/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id, BooksContentsService $service)
    {
        $put = $this->request->only(['img_url','uk_audio_url','en_audio_url','name','profiles','bid','sort','status','phonogram'], 'put');
        $this->validate($put, 'app\admin\validate\erm\BooksContents.update');
        $update = $service->save($id, $put);
        if ( $update === false ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, BooksContentsService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\erm\BooksContents.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-12-20
     * @return \think\Response
     */
    public function status(BooksContentsService $service, BooksContentsModel $model)
    {
        return $service->status($model);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-19
     * @return \think\Response
     */
    public function destory(BooksContentsService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\erm\BooksContents.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
